#Umeda, Michio (2021) "District magnitude and electoral mobilization: How uneven electoral systems shift the focus of campaign efforts by political parties" 
#Japanse Journal of Political Science
#The data management code is available from the author upon request 

#R code for the turnout model
turnout0119 <- read.csv("turnout0119.csv")
#This dataset includes the contents provided by Natori et al. (2016) and Maeda (n.d.). Please cite them if you use this data for publication. 
#Natori et al. (2016) The database for comparative political research. Accessed September 3, 2017. http://db.cps.kutc.kansai-u.ac.jp/
#Maeda (n.d.) The 2016 Election for the Japanese House of Councillors (dataset). Accessed September 3, 2017. http://politicalscience.unt.edu/~maeda/docs/san16dist_public.csv
  
#install.packages("lme4")
library(lme4)

#the random effect model w covariates 
summary(lmer(turnout ~ I(dm!=2) 
  + t0 + sqrt(ncand)
  + p_elderly + p_did 
  + factor(year)
  + (1|prefid), 
  data=turnout0119
)
)

#the fixed effect model w clustered standard error
#install.packages("clubSandwich")
library(clubSandwich)
lm_FE <- lm(
  turnout ~ I(dm!=2) 
  + t0 + sqrt(ncand) 
  + factor(year)
  + factor(prefid),
  data = turnout0119
  )
vcovlm <- vcovCR(lm_FE, cluster = turnout0119$prefid, type = "CR2")
coef_test(lm_FE, vcov = vcovlm)


#R code for the mobilization model
#install.packages("arm")
#install.packages("ordinal")

library(arm)
library(ordinal)

jes34data <- read.csv("jes34data.csv")
#This dataset includes the contents provided by Ikeda et al. (2007) and Hirano et al. (2016). Please cite them if you use this data for publication. 
#Ikeda K et al. (2007) Nation-wide longitudinal survey study on voting behavior in the early 21st century, 2001-2005 https://ssjda.iss.u-tokyo.ac.jp/Direct/gaiyo.php?lang=eng&eid=0530
#Hirano H et al. (2016) Nation-wide longitudinal survey study on voting behavior in an age of political change (JES IV SSJDA version), 2007-2011 (dataset). https://ssjda.iss.u-tokyo.ac.jp/Direct/gaiyo.php?lang=eng&eid=0999.


#use DMD as reference
jes34data$distmag[jes34data$distmag==2] <- 0

model_ldp0110 <- clmm(
  factor(LDPmob_t1) ~ factor(distmag) 
  + factor(LDPmob_t0) + factor(gender) + age + factor(edu) 
  + factor(resyr) + factor(citysize)
  + twocand  + gap_ldp 
  + factor(year) + (1|pref) + (1|id),
  link = "probit", data=jes34data)

model_dpj0110 <- clmm(
  factor(DPJmob_t1) ~ factor(distmag) 
  + factor(DPJmob_t0) + factor(gender) + age + factor(edu) 
  + factor(resyr) + factor(citysize)
  + twocand  + gap_ldp 
  + factor(year) + (1|pref) + (1|id),
  link = "probit", data=jes34data)

model_sml0110 <- clmm(
  factor(SMLmob_t1) ~ factor(distmag) 
  + factor(SMLmob_t0) + factor(gender) + age + factor(edu) 
  + factor(resyr) + factor(citysize)
  + factor(year) + (1|pref) + (1|id),
  link = "probit", data=jes34data)

summary(model_ldp0110)
summary(model_dpj0110)
summary(model_sml0110)

tag_distmag <- c("MMD", "SMD")

coefDPJ <- c(summary(model_dpj0110)$coefficients[5,1],
  summary(model_dpj0110)$coefficients[4,1])
coefLDP <- c(summary(model_ldp0110)$coefficients[5,1],
  summary(model_ldp0110)$coefficients[4,1])
coefSML <- c(summary(model_sml0110)$coefficients[5,1],
  summary(model_sml0110)$coefficients[4,1])
sdDPJ <- c(summary(model_dpj0110)$coefficients[5,2]*1.64,
  summary(model_dpj0110)$coefficients[4,2]*1.64)
sdLDP <- c(summary(model_ldp0110)$coefficients[5,2]*1.64,
  summary(model_ldp0110)$coefficients[4,2]*1.64)
sdSML <- c(summary(model_sml0110)$coefficients[5,2]*1.64,
  summary(model_sml0110)$coefficients[4,2]*1.64)

jpeg("coefplot.jpeg", width = 600, height = 430) 
coefplot(coefSML, sdSML, varnames=tag_distmag, 
  upper.conf.bounds=c(coefSML),
  xlim=c(-0.3, 0.5), cex.pts=1.4, pch.pts=5,
  cex.var=0.9, CI=1, 
  main = "") 

coefplot(coefDPJ, sdDPJ, 
  upper.conf.bounds=c(coefDPJ),
  xlim=c(-0.3, 0.5), cex.pts=1.4, pch.pts=17,
  cex.var=0.9, CI=1, 
  main = "", add=T, offset=0.20) 

coefplot(coefLDP, sdLDP, 
  upper.conf.bounds=c(coefLDP),
  xlim=c(-0.3, 0.5),cex.pts=1.4, pch.pts=19, 
  cex.var=0.9, CI=1, 
  main = "", add=T, offset=0.40)
dev.off() 

options(digits=2)
summary(model_ldp0110)$coefficients[4:5,]
summary(model_dpj0110)$coefficients[4:5,]
summary(model_sml0110)$coefficients[4:5,]

###################################################################